#!/bin/bash


__RSYNC="rsync --rsh='ssh' --archive --compress --progress --human-readable --protect-args --delete "
RSYNC="${__RSYNC} -R " # --max-delete 10 " # --dry-run "

function run_as_user {
	user=kamil
        if test "$UID" -eq '0'; then
                echo "We are running as root, switching to $user with uid $(id -u -z $user)"
        fi
        if test ! "$UID" -eq "$(id -u -z $user)"; then
                exec sudo -u $user $@
                exit 0
        fi
}
function RUN {
        echo $@
        $@
}

zaczektv="kamil@10.8.5.219:/home/users/kamil/backup/"
ustropecet="kamil@10.3.2.42:/home/users/kamil/backup/"
dyziopl="kamil@www.dyzio.pl:/home/kamil/mojdysk/sync/"
list=(
	"--exclude materialy_ogolem /qqq/studia/*"
	"--exclude FUJITSU /qqq/moje/*"
	"/qqq/biblioteka/ksiazki/ /qqq/biblioteka/myprograms/ /qqq/biblioteka/programy/_compiles /qqq/biblioteka/programy/archinstall /qqq/biblioteka/programy/usb_RTL8188EUS /mnt/windows/Users/kamil/Documents/Klei/ /home/pamiec/qqp/"
	"/qqq/studia/materialy_ogolem/inne_pierdoly_od_roberta_rok3_sem5/ /qqq/studia/materialy_ogolem/materiały_dla_potomnych_sem_5/"
	"/etc/"
)

# odpowiedni użytkownik
run_as_user $0 $@

for i in 0 1 2 3 4; do
	$RSYNC ${list[$i]} "$zaczektv"
done
for i in 0 1 2 3 4; do
#        $RSYNC ${list[$i]} "$dyziopl"
	sleep 0.000001
done


exit 0
###### exit



server=10.3.2.42
user=kamil
remoterootdir="$user@$server:/home/users/kamil/sync/"
list=(
	"/qqq/studia/;/qqq/studia"
	"/qqq/moje/;/qqq/moje/"
        "/qqq/biblioteka/ksiazki/;/qqq/biblioteka/ksiazki/"
	"/qqq/biblioteka/myprograms/;/qqq/biblioteka/myprograms/"
	"/qqq/biblioteka/programy/;/qqq/biblioteka/programy/"
	"/mnt/windows/Users/kamil/Documents/Klei/;/gry/Klei/"
	"/home/kamil/eagle/;/eagle/"
	"/home/pamiec/qqp/;/qqp/"
	"/qqq/linux/cod4/players/;/gry/cod4_linux_plyaers/"
	"$user@$server:/home/users/kamil/windows/cod4/players/;/gry/cod4_windows_players/"
	"/etc/;/etc/"
);
SSH="ssh $user@$server -o ConnectTimeout=1 -o PasswordAuthentication=no -o PubkeyAuthentication=yes -o PreferredAuthentications=publickey "
__RSYNC="rsync --rsh='ssh' --archive --compress --progress --human-readable --protect-args --delete --max-delete 10 "
RSYNC="${__RSYNC} " # --dry-run "

function run_as_user {
	if test "$UID" -eq '0'; then
		echo "We are running as root, switching to $user with uid $(id -u -z $user)"
	fi
	if test ! "$UID" -eq "$(id -u -z $user)"; then
		exec sudo -u $user $@
		exit 0
	fi
}
function RUN {
	echo $@
	$@
}
function DEBUG {
	echo "# >>>> "$@
}
function backup {
	for i in ${list[@]}; do
		# list[*} is in for ${localpath};${remotepath}
		#                              ^
		# *path is in form user@server:/dir/dir/dir/
		# *login is in form user@server
		# *dir is in form /dir/dir/dir/
		localpath=$(echo "$i" | cut -d';' -f1)
		remotepath=${remoterootdir}$(echo "$i" | cut -d';' -f2)
		locallogin=$(echo "$localpath" | grep ':' | cut -d':' -f1)
		remotelogin=$(echo "$remotepath" | grep ':' | cut -d':' -f1)
		localdir=$(echo "$localpath" | cut -d':' -f2)
		remotedir=$(echo "$remotepath" | cut -d':' -f2)
		
		# create remote directory if it does not exists
		if $SSH test ! -d "$remotedir"; then
			DEBUG "Remote dir $remotedir does not exists. Creating through ssh"
			RUN $SSH mkdir -p $remotedir
		fi
		
		if test ! -z "$locallogin"; then
			DEBUG "$localpath is not a local dir"
			if test "$locallogin" = "$remotelogin"; then
				DEBUG "$localpath and $remotepath are on the same server, running rsync there!"
				RUN $SSH $RSYNC "$localdir" "$remotedir"
			else
				DEBUG "$localpath and $remotepath are NOT on the same server, running rsync there!"
				tmpdir=$(mktemp -d -t backupXXX)
				DEBUG "copying $localdir to local location $tmpdir"
				###
				DEBUG "copying local location $tmpdir to external location $remotedir"
				###
				rm -r $tmpdir
			fi
			
			continue ###
		fi
		
		if test ! -d "$localdir"; then
			DEBUG "Local dir \"$localdir\" does not exists!!!"
			DEBUG "Omitting"
			continue
		fi
		RUN $RSYNC "$localpath" "$remotepath"
		
		echo
	done
}

echo $UID
# make sure we are runnig as a proper user
run_as_user $0 $@
# backup all
backup

